/**
 * 修改用户信息
 */

import axios from 'axios';
import React from 'react';
import {useLocation, useNavigate} from 'react-router-dom' // eslint-disable-line no-unused-vars

function UpdateUser() {

    const navigate = useNavigate();
    let location =useLocation();
    const json = location.state.nickname.data;
    //提交修改用户信息
    const updateUserSubmit = event =>{
        //取消事件的默认动作
        event.preventDefault();
        //生命头文件
        let config = {headers:{'Content-Type':'multipart-/form-data'},withCredentials:true}
        //进行后台提交
        axios({
            method:'post',
            url:'http://localhost/mall-basic-service/basic/user/updateUser',
            params:{
                userid: event.target.userid.value,
                username: event.target.username.value,
                password: event.target.password.value,
                nickname: event.target.nickname.value,
                type: event.target.type.value,
                phone: event.target.phone.value,
            }
        },config).then((response)=>{
            //成功跳转到用户查询界面
            //请求通过axios请求后台
            axios({
                method: 'get',
                url:'http://localhost/mall-basic-service/basic/user/queryBasicUserLayUiPage',
                params:{
                    page : 1,
                    limit : 10
                }
            },config).then((response)=>{

                //处理回调函数
                navigate('/userList',{
                    state:{ nickname :response.data.data}
                })
            }).catch(function(error){
                //处理异常数据
            })
        }).catch(function(error){
        })
    }


    //根据id 进行查询
    return (
        <div className="bodyDiv">
            <h2>修改用户信息-{json.userid}</h2>
            <form onSubmit={updateUserSubmit}>
                <label>
                    <input className="form-control" type="text" id="userid" name="userid" value={json.userid}/>
                    用户名称:&emsp;
                    <input className="form-control" type="text" id="username" name="username" /></label><br/>
                <label>
                    登录密码:&emsp;
                    <input className="form-control" type="password" id="password" name="password" /></label><br/>
                <label>
                    昵称:&emsp;
                    <input className="form-control" type="text" id="nickname" name="nickname" /></label><br/>
                <label>
                    用户角色:&emsp;
                    <input type="radio" name="type" id="type" value="1" checked="true"/>采购人 &emsp;
                    <input type="radio" name="type" id="type" value="2"/>供应者 &emsp;
                    <input type="radio" name="type" id="type" value="3"/>管理员 &emsp;
                </label><br/>
                <label>
                    手机号码:&emsp;
                    <input className="form-control" type="text" name="phone" id="phone"/>
                </label>
                <br/>
                <label>
                    <button className="btn btn-primary" type="submit">提交</button>
                    &emsp;
                    <button className="btn btn-primary" type="reset">重置</button>
                </label>
            </form>
        </div>
    );
}

export default UpdateUser;